Dynomotion

Group: DynoMotion Message: 14484 From: Moray Cuthill Date: 3/11/2017
Subject: Anyway to get an array length/size?
Hi,

I'm working on a lookup/interpolation program to get my spindle speed output voltage a bit more accurate, and was wondering if there are any functions to get the size of an array?

I've set it manually for now, but it would be good to make it automatic for if I change the number data points in my array of values.

Thanks,
Moray

Virus-free. www.avast.com
Group: DynoMotion Message: 14485 From: TKSOFT Date: 3/11/2017
Subject: Re: Anyway to get an array length/size?
Try sizeof(MyArray)/sizeof(double)

Regards
TK


On 2017-03-11 12:14, Moray Cuthill moray.cuthill@...
[DynoMotion] wrote:
> Hi,
>
> I'm working on a lookup/interpolation program to get my spindle speed
> output voltage a bit more accurate, and was wondering if there are any
> functions to get the size of an array?
>
> I've set it manually for now, but it would be good to make it
> automatic for if I change the number data points in my array of
> values.
>
> Thanks,
> Moray
>
Group: DynoMotion Message: 14486 From: Moray Cuthill Date: 3/11/2017
Subject: Re: Anyway to get an array length/size?
Thanks Tom.

I'm not sure what I was thinking, as I had been trying array.length. I didn't even think about looking at C Programming bible to see what it should be, but I've got it working now.

Thanks,
Moray

Virus-free. www.avast.com

On Sat, Mar 11, 2017 at 9:44 PM, tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Try sizeof(MyArray)/sizeof(double)

Regards
TK

On 2017-03-11 12:14, Moray Cuthill moray.cuthill@...
[DynoMotion] wrote:
> Hi,
>
> I'm working on a lookup/interpolation program to get my spindle speed
> output voltage a bit more accurate, and was wondering if there are any
> functions to get the size of an array?
>
> I've set it manually for now, but it would be good to make it
> automatic for if I change the number data points in my array of
> values.
>
> Thanks,
> Moray
>


Group: DynoMotion Message: 14487 From: Hardy Family Date: 3/12/2017
Subject: Re: Anyway to get an array length/size?
sizeof(MyArray)/sizeof(MyArray[0]) is a little more general (doesn't assume the type of the elements).  And just for fun...

#define ARRAYLENGTH(a) (sizeof(a)/sizeof(a[0]))

so...

ARRAYLENGTH(MyArray)

Regards,
SJH


On Sat, Mar 11, 2017 at 3:09 PM, Moray Cuthill moray.cuthill@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Thanks Tom.

I'm not sure what I was thinking, as I had been trying array.length. I didn't even think about looking at C Programming bible to see what it should be, but I've got it working now.

Thanks,
Moray

Virus-free. www.avast.com

On Sat, Mar 11, 2017 at 9:44 PM, tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Try sizeof(MyArray)/sizeof(double)

Regards
TK

On 2017-03-11 12:14, Moray Cuthill moray.cuthill@...
[DynoMotion] wrote:
> Hi,
>
> I'm working on a lookup/interpolation program to get my spindle speed
> output voltage a bit more accurate, and was wondering if there are any
> functions to get the size of an array?
>
> I've set it manually for now, but it would be good to make it
> automatic for if I change the number data points in my array of
> values.
>
> Thanks,
> Moray
>



Group: DynoMotion Message: 14489 From: Moray Cuthill Date: 3/13/2017
Subject: Re: Anyway to get an array length/size?
Thanks SJH, but I only needed to get the array length once, so Tom's method worked fine.

I've pasted as far as my code got below, as I'll no longer need it. While establishing the compensation values, the spindle motor started acting up, and as I was considering a servo motor anyway, a shiny new servo spindle/drive has been ordered.

There is one major flaw in it, in that if a speed/voltage greater than the second last value in the array is commanded, it sets 0V. It was next on my to fix list having established the compensation/interpolation was working correctly, but now I don't need it, I've got more pressing things needing my time. So I'm just posting this code here as a basis for anybody looking for an interpolation example, as I couldn't find anything specific to the KFlop.
Although having just looked over the code, it may be just a case of either adding an = to the less than in the if, or dropping the -2 in the if... (I'd test it, but I've not got a KFlop handy!).
I had to chop and change the if statement a few times, before realising I couldn't declare the int within the if statement itself, so I may of inadvertently deleted something I didn't mean to...

Moray

float *SpinVolt  = &persist.UserData[SPDLVOLT]; // User persist used to set compensated voltage required
float *SpinSpeed = &persist.UserData[SPDLSPEED]; // User persist where we receive commanded speed from KMotionCNC S command
static int SpinMaxSpeed = 2500;  // Max spindle speed I.e. value at which we want maximum speed/voltage
// The following array has the structure of Desired voltage, Commanded voltage
// to generate this array, change speed command until requested voltage is established
// I.e. to get the desired 1V, in this uneditted file, I needed to request 425RPM which gives 1.70V
// (Keep the console screen open in KMotion, and these values are displayed)
static float SpinCorr[] = {0,0,
1,1.70,
2,2.64,
3,3.50,
4,3.98,
5,5.15,
6,5.92,
7,6.7,
8,7.5,
9,8.35,
10,9.25};

main()
{
 float RawVolt = (*SpinSpeed/SpinMaxSpeed)*10; // calculate desired voltage
 printf("Spindle Speed = %f\n",*SpinSpeed);
 int arrLen = sizeof(SpinCorr)/sizeof(double); // get size of array
 int i= 0;
 float newVolt;
 for(i; i<arrLen-2; i=i+2)
 {
  if ((RawVolt >= SpinCorr[i]) && (RawVolt <= SpinCorr[i+2])) {
   // calculate compensated voltage
   newVolt = SpinCorr[i+1] + ((SpinCorr[i+3]-SpinCorr[i+1]) * ((RawVolt-SpinCorr[i]) / (SpinCorr[i+2]-SpinCorr[i])));
   break;
  }
 }
 printf("Voltage Raw=%f Compensated=%f\n",RawVolt, newVolt);
 *SpinVolt = newVolt; // Store compensated voltage in relevant user persist
}

On Sun, Mar 12, 2017 at 4:27 PM, Hardy Family hardy.woodland.cypress@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

sizeof(MyArray)/sizeof( MyArray[0]) is a little more general (doesn't assume the type of the elements).  And just for fun...

#define ARRAYLENGTH(a) (sizeof(a)/sizeof(a[0]))

so...

ARRAYLENGTH(MyArray)

Regards,
SJH


On Sat, Mar 11, 2017 at 3:09 PM, Moray Cuthill moray.cuthill@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Thanks Tom.

I'm not sure what I was thinking, as I had been trying array.length. I didn't even think about looking at C Programming bible to see what it should be, but I've got it working now.

Thanks,
Moray

Virus-free. www.avast.com

On Sat, Mar 11, 2017 at 9:44 PM, tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Try sizeof(MyArray)/sizeof(double)

Regards
TK

On 2017-03-11 12:14, Moray Cuthill moray.cuthill@...
[DynoMotion] wrote:
> Hi,
>
> I'm working on a lookup/interpolation program to get my spindle speed
> output voltage a bit more accurate, and was wondering if there are any
> functions to get the size of an array?
>
> I've set it manually for now, but it would be good to make it
> automatic for if I change the number data points in my array of
> values.
>
> Thanks,
> Moray
>




Group: DynoMotion Message: 14490 From: Moray Cuthill Date: 3/13/2017
Subject: Re: Anyway to get an array length/size?
And having just had a thought, swap the if to for in those last couple sentences!


Moray

On Mon, Mar 13, 2017 at 11:08 PM, Moray Cuthill <moray.cuthill@...> wrote:
Thanks SJH, but I only needed to get the array length once, so Tom's method worked fine.

I've pasted as far as my code got below, as I'll no longer need it. While establishing the compensation values, the spindle motor started acting up, and as I was considering a servo motor anyway, a shiny new servo spindle/drive has been ordered.

There is one major flaw in it, in that if a speed/voltage greater than the second last value in the array is commanded, it sets 0V. It was next on my to fix list having established the compensation/interpolation was working correctly, but now I don't need it, I've got more pressing things needing my time. So I'm just posting this code here as a basis for anybody looking for an interpolation example, as I couldn't find anything specific to the KFlop.
Although having just looked over the code, it may be just a case of either adding an = to the less than in the if, or dropping the -2 in the if... (I'd test it, but I've not got a KFlop handy!).
I had to chop and change the if statement a few times, before realising I couldn't declare the int within the if statement itself, so I may of inadvertently deleted something I didn't mean to...

Moray

float *SpinVolt  = &persist.UserData[SPDLVOLT]; / / User persist used to set compensated voltage required
float *SpinSpeed = &persist.UserData[SPDLSPEED];  // User persist where we receive commanded speed from KMotionCNC S command
static int SpinMaxSpeed = 2500;  // Max spindle speed I.e. value at which we want maximum speed/voltage
// The following array has the structure of Desired voltage, Commanded voltage
// to generate this array, change speed command until requested voltage is established
// I.e. to get the desired 1V, in this uneditted file, I needed to request 425RPM which gives 1.70V
// (Keep the console screen open in KMotion, and these values are displayed)
static float SpinCorr[] = {0,0,
1,1.70,
2,2.64,
3,3.50,
4,3.98,
5,5.15,
6,5.92,
7,6.7,
8,7.5,
9,8.35,
10,9.25};

main()
{
 float RawVolt = (*SpinSpeed/SpinMaxSpeed)*10;  // calculate desired voltage
 printf("Spindle Speed = %f\n",*SpinSpeed);
 int arrLen = sizeof(SpinCorr)/sizeof( double); // get size of array
 int i= 0;
 float newVolt;
 for(i; i<arrLen-2; i=i+2)
 {
  if ((RawVolt >= SpinCorr[i]) && (RawVolt <= SpinCorr[i+2])) {
   // calculate compensated voltage
   newVolt = SpinCorr[i+1] + ((SpinCorr[i+3]-SpinCorr[i+1]) * ((RawVolt-SpinCorr[i]) / (SpinCorr[i+2]-SpinCorr[i])));
   break;
  }
 }
 printf("Voltage Raw=%f Compensated=%f\n",RawVolt, newVolt);
 *SpinVolt = newVolt; // Store compensated voltage in relevant user persist
}

On Sun, Mar 12, 2017 at 4:27 PM, Hardy Family hardy.woodland.cypress@gmail. com [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

sizeof(MyArray)/sizeof(MyArray [0]) is a little more general (doesn't assume the type of the elements).  And just for fun...

#define ARRAYLENGTH(a) (sizeof(a)/sizeof(a[0]))

so...

ARRAYLENGTH(MyArray)

Regards,
SJH


On Sat, Mar 11, 2017 at 3:09 PM, Moray Cuthill moray.cuthill@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Thanks Tom.

I'm not sure what I was thinking, as I had been trying array.length. I didn't even think about looking at C Programming bible to see what it should be, but I've got it working now.

Thanks,
Moray

Virus-free. www.avast.com

On Sat, Mar 11, 2017 at 9:44 PM, tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Try sizeof(MyArray)/sizeof(double)

Regards
TK

On 2017-03-11 12:14, Moray Cuthill moray.cuthill@...
[DynoMotion] wrote:
> Hi,
>
> I'm working on a lookup/interpolation program to get my spindle speed
> output voltage a bit more accurate, and was wondering if there are any
> functions to get the size of an array?
>
> I've set it manually for now, but it would be good to make it
> automatic for if I change the number data points in my array of
> values.
>
> Thanks,
> Moray
>





Group: DynoMotion Message: 14491 From: Hardy Family Date: 3/14/2017
Subject: Re: Anyway to get an array length/size?
A few observations (just in case anyone needs to do this sort of non-linear mapping):

In the 'for' statement, you have

int i = 0;
...
for (i; i<arrLen-2; i=i+2) ...


which works, but the first 'i' in the 'for' is not doing anything.  The first part is specifically for initialization, so it is more conventional to code

int i;

for (i=0; i<arrLen-2; i=i+2) ...

or

int i = 0;

for (; i<arrLen-2; i=i+2) ...


i.e. you can leave the initialization section empty if initialization is already performed.  Yes, this is arguably pedantic, but C allows you to write statements that do nothing, so it is important not to leave such statements around unless you want to confuse yourself when you come back to the code some time later :-)


Regarding the computed value of '0' for when the input raw voltage is over 10, that is because the for loop can terminate without ever doing anything.  In fact, it is only by luck that you are getting the value 0, since the newVolt variable is not initialized in this case.  You could clamp the input value to 10, or you could handle the case >10 by checking if the loop exited without doing anything.  In your case, you can check the final value of 'i' as in

for (i=0; i<arrLen-2; i=i+2) {
...
}
if (i >= arrLen-2) {
  // never encountered 'break' in the above loop.
  // handle this case using e.g. linear extrapolation
  newVolt = ...
}

Rather than doing this, you could add a final "really big" entry to the table which performs the "extrapolation"...

static float SpinCorr[] = {0,0,
1,1.70,
2,2.64,
3,3.50,
4,3.98,
5,5.15,
6,5.92,
7,6.7,
8,7.5,
9,8.35,
10,9.25,
10000, 9250
};



Regards,
SJH


On Mon, Mar 13, 2017 at 4:36 PM, Moray Cuthill moray.cuthill@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

And having just had a thought, swap the if to for in those last couple sentences!


Moray

On Mon, Mar 13, 2017 at 11:08 PM, Moray Cuthill <moray.cuthill@...> wrote:
Thanks SJH, but I only needed to get the array length once, so Tom's method worked fine.

I've pasted as far as my code got below, as I'll no longer need it. While establishing the compensation values, the spindle motor started acting up, and as I was considering a servo motor anyway, a shiny new servo spindle/drive has been ordered.

There is one major flaw in it, in that if a speed/voltage greater than the second last value in the array is commanded, it sets 0V. It was next on my to fix list having established the compensation/interpolation was working correctly, but now I don't need it, I've got more pressing things needing my time. So I'm just posting this code here as a basis for anybody looking for an interpolation example, as I couldn't find anything specific to the KFlop.
Although having just looked over the code, it may be just a case of either adding an = to the less than in the if, or dropping the -2 in the if... (I'd test it, but I've not got a KFlop handy!).
I had to chop and change the if statement a few times, before realising I couldn't declare the int within the if statement itself, so I may of inadvertently deleted something I didn't mean to...

Moray

float *SpinVolt  = &persist.UserData[SPDLVOLT]; / / User persist used to set compensated voltage required
float *SpinSpeed = &persist.UserData[SPDLSPEED];  // User persist where we receive commanded speed from KMotionCNC S command
static int SpinMaxSpeed = 2500;  // Max spindle speed I.e. value at which we want maximum speed/voltage
// The following array has the structure of Desired voltage, Commanded voltage
// to generate this array, change speed command until requested voltage is established
// I.e. to get the desired 1V, in this uneditted file, I needed to request 425RPM which gives 1.70V
// (Keep the console screen open in KMotion, and these values are displayed)
static float SpinCorr[] = {0,0,
1,1.70,
2,2.64,
3,3.50,
4,3.98,
5,5.15,
6,5.92,
7,6.7,
8,7.5,
9,8.35,
10,9.25};

main()
{
 float RawVolt = (*SpinSpeed/SpinMaxSpeed)*10;  // calculate desired voltage
 printf("Spindle Speed = %f\n",*SpinSpeed);
 int arrLen = sizeof(SpinCorr)/sizeof(double ); // get size of array
 int i= 0;
 float newVolt;
 for(i; i<arrLen-2; i=i+2)
 {
  if ((RawVolt >= SpinCorr[i]) && (RawVolt <= SpinCorr[i+2])) {
   // calculate compensated voltage
   newVolt = SpinCorr[i+1] + ((SpinCorr[i+3]-SpinCorr[i+1]) * ((RawVolt-SpinCorr[i]) / (SpinCorr[i+2]-SpinCorr[i])));
   break;
  }
 }
 printf("Voltage Raw=%f Compensated=%f\n",RawVolt, newVolt);
 *SpinVolt = newVolt; // Store compensated voltage in relevant user persist
}

On Sun, Mar 12, 2017 at 4:27 PM, Hardy Family hardy.woodland.cypress@gmail.c om [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

sizeof(MyArray)/sizeof(MyArray [0]) is a little more general (doesn't assume the type of the elements).  And just for fun...

#define ARRAYLENGTH(a) (sizeof(a)/sizeof(a[0]))

so...

ARRAYLENGTH(MyArray)

Regards,
SJH


On Sat, Mar 11, 2017 at 3:09 PM, Moray Cuthill moray.cuthill@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Thanks Tom.

I'm not sure what I was thinking, as I had been trying array.length. I didn't even think about looking at C Programming bible to see what it should be, but I've got it working now.

Thanks,
Moray

Virus-free. www.avast.com

On Sat, Mar 11, 2017 at 9:44 PM, tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Try sizeof(MyArray)/sizeof(double)

Regards
TK

On 2017-03-11 12:14, Moray Cuthill moray.cuthill@...
[DynoMotion] wrote:
> Hi,
>
> I'm working on a lookup/interpolation program to get my spindle speed
> output voltage a bit more accurate, and was wondering if there are any
> functions to get the size of an array?
>
> I've set it manually for now, but it would be good to make it
> automatic for if I change the number data points in my array of
> values.
>
> Thanks,
> Moray
>






Group: DynoMotion Message: 14492 From: Tom Kerekes Date: 3/15/2017
Subject: Re: Anyway to get an array length/size?

Another observation :)

The correction table is defined as an Array of floats.  The line of code I gave you assumed they would be doubles.  So the line:

int arrLen = sizeof(SpinCorr)/sizeof(double); // get size of array

should probably be:

int arrLen = sizeof(SpinCorr)/sizeof(float); // get size of array


So as written I believe the code is only searching half way through the table.

SJH's method of:

int arrLen = sizeof(SpinCorr)/sizeof(SpinCorr[0]); // get size of array

Is a much better approach for exactly this reason.

Regards
TK

On 3/14/2017 11:05 AM, Hardy Family hardy.woodland.cypress@... [DynoMotion] wrote:
 
A few observations (just in case anyone needs to do this sort of non-linear mapping):

In the 'for' statement, you have

int i = 0;
...
for (i; i<arrLen-2; i=i+2) ...


which works, but the first 'i' in the 'for' is not doing anything.  The first part is specifically for initialization, so it is more conventional to code

int i;

for (i=0; i<arrLen-2; i=i+2) ...

or

int i = 0;

for (; i<arrLen-2; i=i+2) ...


i.e. you can leave the initialization section empty if initialization is already performed.  Yes, this is arguably pedantic, but C allows you to write statements that do nothing, so it is important not to leave such statements around unless you want to confuse yourself when you come back to the code some time later :-)


Regarding the computed value of '0' for when the input raw voltage is over 10, that is because the for loop can terminate without ever doing anything.  In fact, it is only by luck that you are getting the value 0, since the newVolt variable is not initialized in this case.  You could clamp the input value to 10, or you could handle the case >10 by checking if the loop exited without doing anything.  In your case, you can check the final value of 'i' as in

for (i=0; i<arrLen-2; i=i+2) {
...
}
if (i >= arrLen-2) {
  // never encountered 'break' in the above loop.
  // handle this case using e.g. linear extrapolation
  newVolt = ...
}

Rather than doing this, you could add a final "really big" entry to the table which performs the "extrapolation"...

static float SpinCorr[] = {0,0,
1,1.70,
2,2.64,
3,3.50,
4,3.98,
5,5.15,
6,5.92,
7,6.7,
8,7.5,
9,8.35,
10,9.25,
10000, 9250
};



Regards,
SJH


On Mon, Mar 13, 2017 at 4:36 PM, Moray Cuthill moray.cuthill@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 
And having just had a thought, swap the if to for in those last couple sentences!


Moray

On Mon, Mar 13, 2017 at 11:08 PM, Moray Cuthill <moray.cuthill@...> wrote:
Thanks SJH, but I only needed to get the array length once, so Tom's method worked fine.

I've pasted as far as my code got below, as I'll no longer need it. While establishing the compensation values, the spindle motor started acting up, and as I was considering a servo motor anyway, a shiny new servo spindle/drive has been ordered.

There is one major flaw in it, in that if a speed/voltage greater than the second last value in the array is commanded, it sets 0V. It was next on my to fix list having established the compensation/interpolation was working correctly, but now I don't need it, I've got more pressing things needing my time. So I'm just posting this code here as a basis for anybody looking for an interpolation example, as I couldn't find anything specific to the KFlop.
Although having just looked over the code, it may be just a case of either adding an = to the less than in the if, or dropping the -2 in the if... (I'd test it, but I've not got a KFlop handy!).
I had to chop and change the if statement a few times, before realising I couldn't declare the int within the if statement itself, so I may of inadvertently deleted something I didn't mean to...

Moray

float *SpinVolt  = &persist.UserData[SPDLVOLT]; / / User persist used to set compensated voltage required
float *SpinSpeed = &persist.UserData[SPDLSPEED];  // User persist where we receive commanded speed from KMotionCNC S command
static int SpinMaxSpeed = 2500;  // Max spindle speed I.e. value at which we want maximum speed/voltage
// The following array has the structure of Desired voltage, Commanded voltage
// to generate this array, change speed command until requested voltage is established
// I.e. to get the desired 1V, in this uneditted file, I needed to request 425RPM which gives 1.70V
// (Keep the console screen open in KMotion, and these values are displayed)
static float SpinCorr[] = {0,0,
1,1.70,
2,2.64,
3,3.50,
4,3.98,
5,5.15,
6,5.92,
7,6.7,
8,7.5,
9,8.35,
10,9.25};

main()
{
 float RawVolt = (*SpinSpeed/SpinMaxSpeed)*10;  // calculate desired voltage
 printf("Spindle Speed = %f\n",*SpinSpeed);
 int arrLen = sizeof(SpinCorr)/sizeof(double ); // get size of array
 int i= 0;
 float newVolt;
 for(i; i<arrLen-2; i=i+2)
 {
  if ((RawVolt >= SpinCorr[i]) && (RawVolt <= SpinCorr[i+2])) {
   // calculate compensated voltage
   newVolt = SpinCorr[i+1] + ((SpinCorr[i+3]-SpinCorr[i+1]) * ((RawVolt-SpinCorr[i]) / (SpinCorr[i+2]-SpinCorr[i])));
   break;
  }
 }
 printf("Voltage Raw=%f Compensated=%f\n",RawVolt, newVolt);
 *SpinVolt = newVolt; // Store compensated voltage in relevant user persist
}

On Sun, Mar 12, 2017 at 4:27 PM, Hardy Family hardy.woodland.cypress@gmail.c om [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 
sizeof(MyArray)/sizeof(MyArray [0]) is a little more general (doesn't assume the type of the elements).  And just for fun...

#define ARRAYLENGTH(a) (sizeof(a)/sizeof(a[0]))

so...

ARRAYLENGTH(MyArray)

Regards,
SJH


On Sat, Mar 11, 2017 at 3:09 PM, Moray Cuthill moray.cuthill@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 
Thanks Tom.

I'm not sure what I was thinking, as I had been trying array.length. I didn't even think about looking at C Programming bible to see what it should be, but I've got it working now.

Thanks,
Moray

Virus-free. www.avast.com

On Sat, Mar 11, 2017 at 9:44 PM, tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Try sizeof(MyArray)/sizeof(double)

Regards
TK

On 2017-03-11 12:14, Moray Cuthill moray.cuthill@...
[DynoMotion] wrote:
> Hi,
>
> I'm working on a lookup/interpolation program to get my spindle speed
> output voltage a bit more accurate, and was wondering if there are any
> functions to get the size of an array?
>
> I've set it manually for now, but it would be good to make it
> automatic for if I change the number data points in my array of
> values.
>
> Thanks,
> Moray
>







Group: DynoMotion Message: 14493 From: Moray Cuthill Date: 3/16/2017
Subject: Re: Anyway to get an array length/size?
I'm glad some people are observant :-)

My initial run was with the array size hard coded, and it was during a test with the automatic array size calc that the spindle died, long before reaching maximum speed. It had been giving a very occasional rumble, but it started rumbling more and tripping the RCD, and I don't think I made it past the 50% point.

On the plus side, it means I'm gaining two extra Konnect outputs, along with rigid tapping capability (expect questions regarding that soon!)

Thanks,
Moray

Virus-free. www.avast.com

On Wed, Mar 15, 2017 at 6:19 PM, Tom Kerekes tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Another observation :)

The correction table is defined as an Array of floats.  The line of code I gave you assumed they would be doubles.  So the line:

int arrLen = sizeof(SpinCorr)/sizeof( double); // get size of array

should probably be:

int arrLen = sizeof(SpinCorr)/sizeof(float) ; // get size of array


So as written I believe the code is only searching half way through the table.

SJH's method of:

int arrLen = sizeof(SpinCorr)/sizeof( SpinCorr[0]); // get size of array

Is a much better approach for exactly this reason.

Regards
TK


On 3/14/2017 11:05 AM, Hardy Family hardy.woodland.cypress@gmail. com [DynoMotion] wrote:
 
A few observations (just in case anyone needs to do this sort of non-linear mapping):

In the 'for' statement, you have

int i = 0;
...
for (i; i<arrLen-2; i=i+2) ...


which works, but the first 'i' in the 'for' is not doing anything.  The first part is specifically for initialization, so it is more conventional to code

int i;

for (i=0; i<arrLen-2; i=i+2) ...

or

int i = 0;

for (; i<arrLen-2; i=i+2) ...


i.e. you can leave the initialization section empty if initialization is already performed.  Yes, this is arguably pedantic, but C allows you to write statements that do nothing, so it is important not to leave such statements around unless you want to confuse yourself when you come back to the code some time later :-)


Regarding the computed value of '0' for when the input raw voltage is over 10, that is because the for loop can terminate without ever doing anything.  In fact, it is only by luck that you are getting the value 0, since the newVolt variable is not initialized in this case.  You could clamp the input value to 10, or you could handle the case >10 by checking if the loop exited without doing anything.  In your case, you can check the final value of 'i' as in

for (i=0; i<arrLen-2; i=i+2) {
...
}
if (i >= arrLen-2) {
  // never encountered 'break' in the above loop.
  // handle this case using e.g. linear extrapolation
  newVolt = ...
}

Rather than doing this, you could add a final "really big" entry to the table which performs the "extrapolation"...

static float SpinCorr[] = {0,0,
1,1.70,
2,2.64,
3,3.50,
4,3.98,
5,5.15,
6,5.92,
7,6.7,
8,7.5,
9,8.35,
10,9.25,
10000, 9250
};



Regards,
SJH


On Mon, Mar 13, 2017 at 4:36 PM, Moray Cuthill moray.cuthill@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 
And having just had a thought, swap the if to for in those last couple sentences!


Moray

On Mon, Mar 13, 2017 at 11:08 PM, Moray Cuthill <moray.cuthill@...> wrote:
Thanks SJH, but I only needed to get the array length once, so Tom's method worked fine.

I've pasted as far as my code got below, as I'll no longer need it. While establishing the compensation values, the spindle motor started acting up, and as I was considering a servo motor anyway, a shiny new servo spindle/drive has been ordered.

There is one major flaw in it, in that if a speed/voltage greater than the second last value in the array is commanded, it sets 0V. It was next on my to fix list having established the compensation/interpolation was working correctly, but now I don't need it, I've got more pressing things needing my time. So I'm just posting this code here as a basis for anybody looking for an interpolation example, as I couldn't find anything specific to the KFlop.
Although having just looked over the code, it may be just a case of either adding an = to the less than in the if, or dropping the -2 in the if... (I'd test it, but I've not got a KFlop handy!).
I had to chop and change the if statement a few times, before realising I couldn't declare the int within the if statement itself, so I may of inadvertently deleted something I didn't mean to...

Moray

float *SpinVolt  = &persist.UserData[SPDLVOLT]; / / User persist used to set compensated voltage required
float *SpinSpeed = &persist.UserData[SPDLSPEED];  // User persist where we receive commanded speed from KMotionCNC S command
static int SpinMaxSpeed = 2500;  // Max spindle speed I.e. value at which we want maximum speed/voltage
// The following array has the structure of Desired voltage, Commanded voltage
// to generate this array, change speed command until requested voltage is established
// I.e. to get the desired 1V, in this uneditted file, I needed to request 425RPM which gives 1.70V
// (Keep the console screen open in KMotion, and these values are displayed)
static float SpinCorr[] = {0,0,
1,1.70,
2,2.64,
3,3.50,
4,3.98,
5,5.15,
6,5.92,
7,6.7,
8,7.5,
9,8.35,
10,9.25};

main()
{
 float RawVolt = (*SpinSpeed/SpinMaxSpeed)*10;  // calculate desired voltage
 printf("Spindle Speed = %f\n",*SpinSpeed);
 int arrLen = sizeof(SpinCorr)/sizeof(double ); // get size of array
 int i= 0;
 float newVolt;
 for(i; i<arrLen-2; i=i+2)
 {
  if ((RawVolt >= SpinCorr[i]) && (RawVolt <= SpinCorr[i+2])) {
   // calculate compensated voltage
   newVolt = SpinCorr[i+1] + ((SpinCorr[i+3]-SpinCorr[i+1]) * ((RawVolt-SpinCorr[i]) / (SpinCorr[i+2]-SpinCorr[i])));
   break;
  }
 }
 printf("Voltage Raw=%f Compensated=%f\n",RawVolt, newVolt);
 *SpinVolt = newVolt; // Store compensated voltage in relevant user persist
}

On Sun, Mar 12, 2017 at 4:27 PM, Hardy Family hardy.woodland.cypress@gmail.c om [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 
sizeof(MyArray)/sizeof(MyArray [0]) is a little more general (doesn't assume the type of the elements).  And just for fun...

#define ARRAYLENGTH(a) (sizeof(a)/sizeof(a[0]))

so...

ARRAYLENGTH(MyArray)

Regards,
SJH


On Sat, Mar 11, 2017 at 3:09 PM, Moray Cuthill moray.cuthill@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 
Thanks Tom.

I'm not sure what I was thinking, as I had been trying array.length. I didn't even think about looking at C Programming bible to see what it should be, but I've got it working now.

Thanks,
Moray

Virus-free. www.avast.com

On Sat, Mar 11, 2017 at 9:44 PM, tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Try sizeof(MyArray)/sizeof(double)

Regards
TK

On 2017-03-11 12:14, Moray Cuthill moray.cuthill@...
[DynoMotion] wrote:
> Hi,
>
> I'm working on a lookup/interpolation program to get my spindle speed
> output voltage a bit more accurate, and was wondering if there are any
> functions to get the size of an array?
>
> I've set it manually for now, but it would be good to make it
> automatic for if I change the number data points in my array of
> values.
>
> Thanks,
> Moray
>